package com.F01.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.F01.actionForm.F010102ActionForm;
import com.F01.dao.F010102DAO;
import com.action.BaseAction;
import com.config.Constants;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;
import com.util.SessionUtils;

/**
 * @wangk071226
 * @订单信息管理
 * @功能：初始化，查询，新增，修改，删除�?
 * @规范：execute()方法制作判断转发，不处理具体业务�? @ ACTION中所有�?�辑判断对应的具体业务方法的命名规则
 * do+静态参数，例如:operationType=init，实现方法为doINIT(). @ 涉及到连接数据库的方法全部写在DAO中�
 * @操作记录：对于insert，update，delete操作系统�?要记录�??
 * @调试信息打印使用LOG.INFO();
 * @记得加注释
 * 
 */
public class F010102Action extends BaseAction {
	/** 记录操作日志 */
	Logger log = Logger.getLogger(F010102Action.class);

	/** 操作记录INFO */
	T_S_YHCZXXinfo t_YhczxxInfo = null;

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		/** 获得actionForm对象 */
		F010102ActionForm actionForm = (F010102ActionForm) form;
		/** 获得operationType值�?�做执行判断�? */
		String operationType = actionForm.getOperationType();
		String language = SessionUtils.getLocale(request);
		actionForm.setLanguage(language);
		log.info("operationType = " + operationType);
		/** 获取实例 */
		F010102DAO dao = new F010102DAO();
		try {
			/** 生成操作记录语句 */
			t_YhczxxInfo = setYhczxx(request, response, operationType,
					"D010101");
			/**
			 * @页面初始�?
			 * @统一参数：operationType=init
			 * @规范：左树进入连接页面不执行查询�?
			 */
			if (Constants.INIT.equals(operationType)) {
				this.doINIT(actionForm,dao);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @查询功能
			 * @统一参数：operationType=queryList
			 * @规范：查询后回显查询条件。如果查询结果无记录，在页面给出提示�?
			 */
			if (Constants.QUERYLIST.equals(operationType)) {
				this.doQUERYLIST(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @新增功能
			 * @统一参数：operationType=add
			 * @规范：进入新增页面�??
			 */
			if (Constants.ADD.equals(operationType)) {
				this.doADD(actionForm, dao, request);
				return mapping.findForward(Constants.detail);
			}
			/**
			 * @新增保存功能
			 * @统一参数：operationType=insert
			 * @规范：新增保存后，返回查询页面，更新记录，给出用户提示信息�?��??
			 */
			if (Constants.SAVE_INSERT.equals(operationType)) {
				this.doSAVE_INSERT(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @编辑功能
			 * @统一参数：operationType=lookparticular
			 * @规范：进入修改页面�??
			 */
			if (Constants.EDIT.equals(operationType)) {
				this.doEDIT(actionForm, dao, request);
				return mapping.findForward(Constants.detail);
			}
			/**
			 * @查看详细信息功能
			 * @统一参数：operationType=edit
			 * @规范：进入修改页面�??
			 */
			if (Constants.LOOKPARTICULAR.equals(operationType)) {
				this.doEDIT(actionForm, dao, request);
				return mapping.findForward(Constants.particular);
			}
			/**
			 * @编辑保存功能
			 * @统一参数：operationType=update
			 * @规范：修改保存后，返回查询页面，更新记录，给出用户提示信息�??
			 */
			if (Constants.SAVE_UPDATE.equals(operationType)) {
				this.doSAVE_UPDATE(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @删除功能
			 * @统一参数：operationType=delete
			 * @规范：更新记录，给出用户提示信息�?
			 */
			if (Constants.DELETE.equals(operationType)) {
				this.doDELETE(actionForm, dao, request);
				return mapping.findForward(Constants.List);
			}
			/**
			 * @返回List页面
			 * @统一参数：operationType=back
			 * @规范：返回LIST页面，显示查询结果
			 * 
			 */
			if (Constants.BACK.equals(operationType)) {
				this.doActionForm(actionForm, dao, request);
				actionForm.setErrorFlag("");
				return mapping.findForward(Constants.List);
			}
			/**
			 * @check
			 * @统一参数：operationType=check
			 * @返回boolean值
			 */
			if (Constants.CHECK.equals(operationType)) {
				/** EXT AJAX */
				StringBuffer sb = new StringBuffer();
				String bs = "0";
				sb.append("{success:true, data:{");
				request.setCharacterEncoding("UTF-8");
				response.setCharacterEncoding("UTF-8");
				String manufacturing_order = request
						.getParameter("manufacturing_order");
				
				if(dao.Check_Cotract_Manufacturing_number(manufacturing_order)){
					bs = "1";
				}
				sb.append("value1:").append(bs);
				response.getWriter().write(
						sb.substring(0, sb.length()).concat("}}"));
				log.info(sb.substring(0, sb.length()).concat("}}"));
				return null;
			}
			
			// 动态获取客户的名称和编码
			if (Constants.AJAX.equals(operationType)) {
				this.initList( dao, request, response);
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null)
				dao.disConnect();
		}
		return mapping.findForward(Constants.INIT);
	}

	// 动态获取客户的名称和编码
	public void initList( F010102DAO dao,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			String name = CommonTools.trim(request
					.getParameter("name"));
			this.setFilterList(dao.filterList("c_customers", "customers_code", "customers_name", "customers_name like '%" + name + "%'"), response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @初始�?
	 */
	public void doINIT(F010102ActionForm actionForm,F010102DAO dao) throws SelectException {
		actionForm.setList_c_customer_type(dao.List_c_customer_type());
		actionForm.setList_t_product_type(dao.List_t_product_type());
		actionForm.setList_state_type(dao.List_state_type());
		actionForm.setCurrentPage(1);
	}

	/**
	 * @查询
	 */
	private void doQUERYLIST(F010102ActionForm actionForm, F010102DAO dao,
			HttpServletRequest request) throws SelectException {
		actionForm.setList_c_customer_type(dao.List_c_customer_type());
		actionForm.setList_t_product_type(dao.List_t_product_type());
		actionForm.setList_state_type(dao.List_state_type());
		List list = new ArrayList();
		actionForm.setList(list);
		list = dao.QUERYLIST(actionForm, request);
		if (list != null && list.size() > 0) {
			actionForm.setList(list);
		} else {
			actionForm.setErrorFlag("2");
		}
	}

	/**
	 * @新增
	 */
	private void doADD(F010102ActionForm actionForm, F010102DAO dao,
			HttpServletRequest request) throws SelectException {
		actionForm.setCotract_number("");
		actionForm.setSignaure_date("");
		actionForm.setManufacturing_order("");
		actionForm.setModel("");
		actionForm.setDescription("");
		actionForm.setQuantity("");
		actionForm.setVAT("");
		actionForm.setUnit_price("");
		actionForm.setTotal_price("");
		actionForm.setTransport("");
		actionForm.setCommission("");
		actionForm.setCustomer_delivery_date("");
		actionForm.setPayment_terms("");
		actionForm.setBank_guaranty("");
		actionForm.setBond_date("");
		actionForm.setBond_value("");
		actionForm.setCustomer_code("");
		actionForm.setArea("");
		actionForm.setSaleman_code("");
		actionForm.setPassing_date("");
		actionForm.setArea_group("");
		actionForm.setOwn_production("");
		actionForm.setRemark("");
		actionForm.setCustomer_type("");
		actionForm.setProduct_type("");
		actionForm.setSignaure_date_begin("");
		actionForm.setSignaure_date_end("");
		actionForm.setCustomer_delivery_date_begin("");
		actionForm.setCustomer_delivery_date_end("");
		actionForm.setList_c_customer_type(dao.List_c_customer_type());
		actionForm.setList_t_product_type(dao.List_t_product_type());
		actionForm.setList_c_area_group(dao.List_c_area_group());
		actionForm.setList_t_product_type(dao.List_t_product_type());
		actionForm.setList_c_saleman(dao.List_c_saleman());
		actionForm.setList_c_payment_terms(dao.List_c_payment_terms());
		actionForm.setOperationType(Constants.SAVE_INSERT);
	}

	/**
	 * @新增保存
	 * @执行新增后，清空actionForm，执行查询操作，返回查询界面。
	 * 
	 */
	private void doSAVE_INSERT(F010102ActionForm actionForm, F010102DAO dao,
			HttpServletRequest request) throws SelectException {
		dao.SAVE_INSERT(actionForm, t_YhczxxInfo);
		this.doActionForm(actionForm, dao, request);
	}

	/**
	 * @编辑
	 */
	private void doEDIT(F010102ActionForm actionForm, F010102DAO dao,
			HttpServletRequest request) throws SelectException {
		dao.EDIT(actionForm, request);
		actionForm.setList_c_customer_type(dao.List_c_customer_type());
		actionForm.setList_t_product_type(dao.List_t_product_type());
		actionForm.setList_c_area_group(dao.List_c_area_group());
		actionForm.setList_t_product_type(dao.List_t_product_type());
		actionForm.setList_c_saleman(dao.List_c_saleman());
		actionForm.setList_c_payment_terms(dao.List_c_payment_terms());
		actionForm.setOperationType(Constants.SAVE_UPDATE);
	}

	/**
	 * @编辑保存
	 */
	private void doSAVE_UPDATE(F010102ActionForm actionForm, F010102DAO dao,
			HttpServletRequest request) throws SelectException {
		dao.SAVE_UPDATE(actionForm, t_YhczxxInfo);
		this.doActionForm(actionForm, dao, request);
	}

	/**
	 * @删除
	 */
	private void doDELETE(F010102ActionForm actionForm, F010102DAO dao,
			HttpServletRequest request) throws SelectException {
		dao.DELETE(actionForm, t_YhczxxInfo, request);
		this.doActionForm(actionForm, dao, request);
	}

	/**
	 * @throws SelectException
	 * @清空actionForm
	 * @执行查询回显操作
	 */
	private void doActionForm(F010102ActionForm actionForm, F010102DAO dao,
			HttpServletRequest request) throws SelectException {

		actionForm.setCotract_number("");
		actionForm.setSignaure_date("");
		actionForm.setManufacturing_order("");
		actionForm.setModel("");
		actionForm.setDescription("");
		actionForm.setQuantity("");
		actionForm.setVAT("");
		actionForm.setUnit_price("");
		actionForm.setTotal_price("");
		actionForm.setTransport("");
		actionForm.setCommission("");
		actionForm.setCustomer_delivery_date("");
		actionForm.setPayment_terms("");
		actionForm.setBank_guaranty("");
		actionForm.setBond_date("");
		actionForm.setBond_value("");
		actionForm.setCustomer_code("");
		actionForm.setCustomer_name("");
		actionForm.setArea("");
		actionForm.setSaleman_code("");
		actionForm.setPassing_date("");
		actionForm.setArea_group("");
		actionForm.setOwn_production("");
		actionForm.setRemark("");
		actionForm.setCustomer_type("");
		actionForm.setProduct_type("");
		actionForm.setSignaure_date_begin("");
		actionForm.setSignaure_date_end("");
		actionForm.setCustomer_delivery_date_begin("");
		actionForm.setCustomer_delivery_date_end("");
		actionForm.setOperationType("");
		this.doQUERYLIST(actionForm, dao, request);
		actionForm.setList_c_customer_type(dao.List_c_customer_type());
		actionForm.setList_t_product_type(dao.List_t_product_type());
	}
}
